package com.hcinfos.gsoo.adapter;

import java.math.BigDecimal;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hcinfos.gsoo.GsooApplication;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Constants;
import com.hcinfos.gsoo.enity.GdSupplerBasicConfExt;
import com.hcinfos.gsoo.enity.ShopInfo;
import com.hcinfos.gsoo.enity.ViewSuppSearchExt;
import com.hcinfos.gsoo.util.BitmapHelp;
import com.hcinfos.gsoo.util.CommonUtil;
import com.hcinfos.gsoo.util.DateUtil;
import com.hcinfos.gsoo.util.ImageUtil;
import com.hcinfos.gsoo.util.ImageUtil.ImageCallback;
import com.hcinfos.gsoo.util.LogUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.db.sqlite.CursorUtils.FindCacheSequence;

public class DingzuoListAdapter extends BaseAdapter {

	private List<GdSupplerBasicConfExt> list;
	private LayoutInflater mInflater;
	public int index = 0;
	private Context context;
	private BitmapDisplayConfig bigPicDisplayConfig;
	private BitmapUtils bitmapUtils;

	public DingzuoListAdapter(Context context, List<GdSupplerBasicConfExt> list) {
		this.list = list;
		this.context = context;
		mInflater = LayoutInflater.from(context);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadingDrawable(context.getResources()
				.getDrawable(R.drawable.tc_shop_default_logo));
		bigPicDisplayConfig.setLoadFailedDrawable(context.getResources()
				.getDrawable(R.drawable.tc_shop_default_logo));
		bitmapUtils = BitmapHelp.getBitmapUtils(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final TypeHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.adapter_dingzuo_main_list_item, null);
			holder = new TypeHolder();
			convertView.setTag(holder);
			holder.icon_iv = (ImageView) convertView.findViewById(R.id.icon_iv);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
			holder.send_money_tv = (TextView) convertView
					.findViewById(R.id.send_money_tv);
			holder.start_money_tv = (TextView) convertView
					.findViewById(R.id.start_money_tv);
			holder.distance_tv = (TextView) convertView
					.findViewById(R.id.distance_tv);
			holder.state_tv = (TextView) convertView
					.findViewById(R.id.state_tv);
		} else {
			holder = (TypeHolder) convertView.getTag();
		}
		GdSupplerBasicConfExt ext = list.get(position);
		bitmapUtils.display(holder.icon_iv, ext.getLogoImgAddr(),
				bigPicDisplayConfig);
		holder.name_tv.setText(ext.getSuppName());
		double distance = 0;
		try {
			distance = CommonUtil.round(ext.getDistance(), 2);
		} catch (Exception e) {
			distance = 0;
		}
		holder.distance_tv.setText(distance + " km");
		holder.time_tv.setText("营业时间:" + ext.getSuppBusinessTime());
		holder.send_money_tv.setVisibility(View.GONE);
		holder.start_money_tv.setText(ext.getOtablePrice() == null
				|| ext.getOtablePrice() < 0 ? "0" : ext.getOtablePrice()
				+ "元 起订");
		holder.state_tv.setVisibility(View.GONE);
		return convertView;
	}

	private final class TypeHolder {

		ImageView icon_iv;
		TextView name_tv;
		TextView time_tv;
		TextView send_money_tv;
		TextView start_money_tv;
		TextView distance_tv;
		TextView state_tv;

	}

	public void setDataChange(List<GdSupplerBasicConfExt> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
